package com.base.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.log.domain.bo.TSysLogBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.log.domain.vo.TSysLogVo;
import com.base.log.domain.TSysLog;
import com.base.log.mapper.TSysLogMapper;
import com.base.log.service.ITSysLogService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 操作日志Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysLogServiceImpl implements ITSysLogService {

    private final TSysLogMapper baseMapper;

    /**
     * 查询操作日志
     */
    @Override
    public TSysLogVo queryById(String logId){
        return baseMapper.selectVoById(logId);
    }

    /**
     * 查询操作日志列表
     */
    @Override
    public TableDataInfo<TSysLogVo> queryPageList(TSysLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysLog> lqw = buildQueryWrapper(bo);
        Page<TSysLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询操作日志列表
     */
    @Override
    public List<TSysLogVo> queryList(TSysLogBo bo) {
        LambdaQueryWrapper<TSysLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysLog> buildQueryWrapper(TSysLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOperateType()), TSysLog::getOperateType, bo.getOperateType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperateFunction()), TSysLog::getOperateFunction, bo.getOperateFunction());
        lqw.eq(StringUtils.isNotBlank(bo.getOperateDescribe()), TSysLog::getOperateDescribe, bo.getOperateDescribe());
        lqw.eq(bo.getOperateTime() != null, TSysLog::getOperateTime, bo.getOperateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TSysLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getArchive()), TSysLog::getArchive, bo.getArchive());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditor()), TSysLog::getAuditor, bo.getAuditor());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditRemark()), TSysLog::getAuditRemark, bo.getAuditRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), TSysLog::getAuditStatus, bo.getAuditStatus());
        lqw.eq(bo.getAuditTime() != null, TSysLog::getAuditTime, bo.getAuditTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSignFlag()), TSysLog::getSignFlag, bo.getSignFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getSignValue()), TSysLog::getSignValue, bo.getSignValue());
        lqw.eq(StringUtils.isNotBlank(bo.getVerStatus()), TSysLog::getVerStatus, bo.getVerStatus());
        return lqw;
    }

    /**
     * 新增操作日志
     */
    @Override
    public Boolean insertByBo(TSysLogBo bo) {
        TSysLog add = BeanUtil.toBean(bo, TSysLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLogId(add.getLogId());
        }
        return flag;
    }

    /**
     * 修改操作日志
     */
    @Override
    public Boolean updateByBo(TSysLogBo bo) {
        TSysLog update = BeanUtil.toBean(bo, TSysLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除操作日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
